﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NUnit.Framework;
using ECCIT;
using OgilvyOne.UKNow.Model;
using OgilvyOne.UKNow.IDAL;
using OgilvyOne.UKNow.DALFactory;

namespace OgilvyOne.UKNow.DAL.Test
{
    [TestFixture]
    [Category("")]
    public class SponsorDALTest
    {
        private SponsorModel CreateNewModel()
        {
            SponsorModel model = new SponsorModel();
            model.ID = null;
            model.LanguageID = 1033;
            model.Detail = "Detail";
            model.InsertedTime = DateTime.Now;
            model.Name = "Name";
            model.Order = 1;
            model.State = 1;
            model.UpdatedTime = DateTime.Now;

            return model;
        }

        [Test]
        public void TestSaveInsert()
        {
            SponsorModel model = CreateNewModel();
            IDALSponsor dal = SponsorDALFactory.Create(1033);
            try
            {
                ResultInfo result = dal.Save(model);
                Assert.AreEqual(true, result.Result);
                Assert.AreNotEqual(null, result.ReturnObject);
                Assert.AreNotEqual(0, ((SponsorModel)result.ReturnObject).ID);
            }
            catch (Exception ex)
            {
                Assert.Fail("Test: TestSaveInsert failed with exception: " + ex.Message);
            }
        }

        [Test]
        public void TestSaveUpdate()
        {
            SponsorModel model = CreateNewModel();
            IDALSponsor dal = SponsorDALFactory.Create(1033);
            try
            {
                ResultInfo result = dal.Save(model);
                Assert.AreEqual(true, result.Result);

                model.Detail = "This is updated";
                result = dal.Save(model);
                Assert.AreEqual(true, result.Result);
            }
            catch (Exception ex)
            {
                Assert.Fail("Test: TestSaveUpdate failed with exception: " + ex.Message);
            }
        }

        [Test]
        public void TestGetSponsorModelByID()
        {
            SponsorModel model = CreateNewModel();
            IDALSponsor dal = SponsorDALFactory.Create(1033);
            try
            {
                ResultInfo result = dal.Save(model);
                Assert.AreEqual(true, result.Result);

                SponsorModel retrievedModel = dal.GetSponsorModelByID(model.ID);
                Assert.AreNotEqual(null, retrievedModel);
                Assert.AreEqual(model.ID, retrievedModel.ID);
                Assert.AreEqual(model.Name, retrievedModel.Name);
                Assert.AreEqual(model.InsertedTime.ToString("yyyy-MM-dd hh:mm:ss"), retrievedModel.InsertedTime.ToString("yyyy-MM-dd hh:mm:ss"));
                Assert.AreEqual(model.Order, retrievedModel.Order);
                Assert.AreEqual(model.Detail, retrievedModel.Detail);
            }
            catch (Exception ex)
            {
                Assert.Fail("Test: TestGetSponsorModelByID failed with exception: " + ex.Message);
            }
        }

        [Test]
        public void TestDelete()
        {
            SponsorModel model = CreateNewModel();
            IDALSponsor dal = SponsorDALFactory.Create(1033);
            try
            {
                ResultInfo result = dal.Save(model);
                Assert.AreEqual(true, result.Result);

                IList<int?> idList = new List<int?>();
                idList.Add(model.ID);
                result = dal.Delete(idList);
                Assert.AreEqual(true, result.Result);
            }
            catch (Exception ex)
            {
                Assert.Fail("Test: TestDelete failed with exception: " + ex.Message);
            }
        }
    }
}
